package com.smart.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.Salary;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.resp.SalaryResp;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author lyj
 * @since 2023-05-17
 */
public interface SalaryMapper extends BaseMapper<Salary> {

    @Select("select s.id,\n" +
            "s.salary_date,\n" +
            "s.base_amount,\n" +
            "s.user_id,\n" +
            "ifnull(s.reimbursement_amount,0) reimbursement_amount,\n" +
            "ifnull(s.leave_amount,0) leave_amount,\n" +
            "ifnull(s.rd_amount,0) rd_amount,\n" +
            "ifnull(s.sales_amount,0) sales_amount,\n" +
            "ifnull(s.bonus,0) bonus,\n" +
            "ifnull(s.penalty,0) penalty,\n" +
            "s.status,\n" +
            "s.create_by,\n" +
            "s.create_date,\n" +
            "s.update_by,\n" +
            "s.update_date,\n" +
            "s.del_flag,\n" +
            "u.username username,\n" +
            "r.name roleName,\n" +
            "(s.base_amount +\n" +
            "ifnull(reimbursement_amount,0) -\n" +
            "ifnull(leave_amount,0) +\n" +
            "ifnull(rd_amount,0) +\n" +
            "ifnull(sales_amount+bonus,0) -\n" +
            "ifnull(penalty,0)) sumSalary\n" +
            "from salary s\n" +
            "left join user u\n" +
            "on s.user_id=u.id\n" +
            "left join role r\n" +
            "on u.role_id=r.id ${ew.customSqlSegment}")
    Page<SalaryResp> queryByCondition(@Param("ew") QueryWrapper<SalaryResp> wrapper, Page<SalaryResp> page);

    @Select("select id,\n" +
            "salary_date,\n" +
            "base_amount,\n" +
            "user_id,\n" +
            "ifnull(reimbursement_amount,0) reimbursement_amount,\n" +
            "ifnull(leave_amount,0) leave_amount,\n" +
            "ifnull(rd_amount,0) rd_amount,\n" +
            "ifnull(sales_amount,0) sales_amount,\n" +
            "ifnull(bonus,0) bonus,\n" +
            "ifnull(penalty,0) penalty,\n" +
            "status,\n" +
            "create_by,\n" +
            "create_date,\n" +
            "update_by,\n" +
            "update_date,\n" +
            "del_flag\n" +
            "from salary\n" +
            "where month(salary_date)=(month(now())-1)\n" +
            "and year(salary_date)=year(now())\n" +
            "and status=0")
    List<Salary> queryNotPaySalaries();

    @Select("select id,\n" +
            "salary_date,\n" +
            "base_amount,\n" +
            "user_id,\n" +
            "ifnull(reimbursement_amount,0) reimbursement_amount,\n" +
            "ifnull(leave_amount,0) leave_amount,\n" +
            "ifnull(rd_amount,0) rd_amount,\n" +
            "ifnull(sales_amount,0) sales_amount,\n" +
            "ifnull(bonus,0) bonus,\n" +
            "ifnull(penalty,0) penalty,\n" +
            "status,\n" +
            "create_by,\n" +
            "create_date,\n" +
            "update_by,\n" +
            "update_date,\n" +
            "del_flag\n" +
            "from salary\n" +
            "where user_id=#{userId}\n" +
            "and status=0\n" +
            "and year(now())=year(salary_date)\n" +
            "and month(now())=month(now())")
    List<Salary> getByUserId(Long userId);
}
